package com.bigdata.demo.controller;

import com.auth0.jwt.algorithms.Algorithm;
import com.bigdata.demo.api.CommonResult;
import com.bigdata.demo.dao.LoginDao;
import com.bigdata.demo.dao.UserDao;
import com.bigdata.demo.entity.User;
import com.bigdata.demo.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.auth0.jwt.JWT;

import com.bigdata.demo.jwtToken.PassToken;
import com.bigdata.demo.jwtToken.CheckToken;
import com.bigdata.demo.entity.User;
import com.bigdata.demo.service.TokenService;
import com.bigdata.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController //用来表示这是一个对外的接口
@CrossOrigin("*")

public class Login {
//        这是大数据平台接口设计——容器创建——container_create_test
//    @RequestMapping("/docker/container_create")
//    @PostMapping("/docker/image/add_image_copy/")
//    public boolean login(
//            @RequestParam("username") String username,
//            @RequestParam("password") String password
//    ) {
////        1. 从数据库中获取用户
//        User user = new LoginDao() {
//            @Override
//            public User find_user_by_name(String username) {
//                return null;
//            }
//        }.find_user_by_name(username);
//        if (user.getPassword().equals(password)) {
//            return true;
//        }
//        return false;
//    }
//}
    @Autowired
    private LoginDao loginDao;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonResult login(@RequestParam("username") String username,
                              @RequestParam("password") String password
    ) {

        User user = loginDao.find_user_by_name(username);

        if(user.getUsername().equals(username) && user.getPassword().equals(password)){
            return CommonResult.success("登录成功");
        }else {
            return CommonResult.failed();
        }
    }


    /**
     * 使用token
     * 查库使用Spring Security的BCryptPasswordEncoder进行密码校验
     */
//       @Autowired
//        UserService userService;
//        @Autowired
//        TokenService tokenService;
//
//        @Value("${EXPIRE_TIME}")
//        private String EXPIRE_TIME;
//
//        @CheckToken
//        //@GetMapping("")
//        public String getUser(@PathVariable("userName") String userName) {
//            User userByName = userService.getUserByName(userName);
//            return userByName.toString();
//
//        }
//        //登录
//
//        //@PostMapping("")
//        public Map<String, Object> login(@RequestBody Map user){
//            Map result=new HashMap();
//            User userForBase=userService.getUserByName(String.valueOf(user.get("username")));
//            if(userForBase==null){
//
//                result.put("message","登录失败,用户不存在");
//                return result;
//            }else {
//                BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
//                String dbPwd=userForBase.getPassword();
//                boolean matchesResult = bCryptPasswordEncoder.matches(String.valueOf(user.get("password")),dbPwd);
//                if (!matchesResult){
//                    result.put("message","登录失败,密码错误");
//                    return result;
//                }else {
//                   //EXPIRE_TIME在yml文件中
//                    Date expiresDate = new Date(System.currentTimeMillis()+Integer.valueOf(EXPIRE_TIME)*60*1000);
//
//                    String token = tokenService.getToken(userForBase,expiresDate);
//                    result.put("token", token);
//                    result.put("expireTime", EXPIRE_TIME);
//                    result.put("userId", userForBase.getUser_num());
//                    return result;
//                }
//            }
//        }
//       @CheckToken
//        @GetMapping("")
//        public String afterLogin(){
//
//            return "你已通过验证,成功进入系统";
//        }

}